<?php
define ('TEST_USER_ID', 2);
require_once '../common.php';

if ($_POST['item_image'])
{
	$post_info = $_POST;
	
	$item_image_name = $post_info['item_image'];
	$item_image_original = ITEM_IMAGE_UPLOAD_DIR.'/'.$item_image_name;
	if (!file_exists($item_image_original))
	{
		echo json_encode(array("result"=>true));
	}
	
	$item_x = $post_info['item_x'];
	$item_y = $post_info['item_y'];
	$item_id = sys::sql("insert into test_items (item_image, item_x, item_y)"
		." values ('%s', '%d', '%d') on duplicate key update item_image='%s';",
		$item_image_name, $item_x, $item_y, $item_image_name);
	if (!$item_id) echo json_encode(array("result"=>true));

	if (itemimage::save($item_id, $item_image_original))
	{
		echo json_encode(array("result"=>true, "item_id"=>$item_id));
	}
}

/*
CREATE TABLE `test_items` (
  `item_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `item_image` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `item_x` int(11) unsigned,
  `item_y` int(11) unsigned,
  PRIMARY KEY (`item_id`),
  KEY (`item_x`),
  KEY (`item_y`),
  UNIQUE KEY item_x_y (`item_x`, `item_y`)
) ENGINE=MyISAM AUTO_INCREMENT=10000 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

*/